#-*-coding:utf-8-*-
'''
Created on 2011-2-13

@author: zhaoyh
'''

import web
from admin.config import view
from db.org import Employee,EmployeeType
from core.meta import PageList,DBSession
from sqlalchemy.exc import IntegrityError 
from utils.utility import AppStatus
status=AppStatus()
compo_name='职员'
compo_title={
            'ee_code':'代码',
            'order_code':' 排序号',
            'ee_name':'姓名',
            'dept_id':'部门',
            'is_user':'是否登陆',
            'user_id':'登陆名',
            'ee_birthdate':'生日',
            'et_id':'类别',
            'ee_sex':' 性别',
            'ee_email':'email',
            'ee_im':' IM号',
            'ee_address':' 地址',
            'ee_telphone':'电话',
            'ee_edu':'文化程度',
            'ee_mobile':'移动电话',
            'is_active':'启用',
            'ee_notes':'备注',
        }
class list():
    '''
         请求页面时使用get，提交数据时使用post
    '''
    def GET(self,name):
        page=web.input(numPerPage=20,pageNum=1,keywords='',orderField='order_code',col='')
        if page.col=='' or page.col==None:
            col=['ee_code','ee_name']
        else:
            col=[page.col]
        list=PageList(Employee,page,page.orderField,page.numPerPage,page.pageNum,col,page.keywords)      
        return view.employee_list(list=list.getlist(),page=list.showPage(),title=compo_title)        
class add():
    def GET(self):
        et=DBSession.query(EmployeeType).order_by(EmployeeType.et_code)
        #g=group.getQuery('group_code', limit, offset, col, keyword)
        return view.employee_add(et=et)
    def POST(self):
        u=web.input(is_lock='N')
        new=Employee(u.user_id,u.user_name,hash_password(u.user_pwd),u.user_email,u.is_lock)
        if u.group_id<>u'':new_user.group_id=u.group_id
        try:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
            DBSession.add(new)            
            DBSession.commit()
            DBSession.flush()
            status.statusCode="ok"
            status.message="添加成功"
            status.navTabId="user_list"
            #return '{"statusCode":"200","message":"添加成功","navTabId":"user_list","dialogid":"user_add", "callbackType":"forward","dialogUrl":"/admin/user/add"}'
        except IntegrityError:
            DBSession.rollback()
            status.statusCode="error"
            status.message="用户名重复"
        except Exception,e:
            print Exception,e
            DBSession.rollback()
            status.statusCode="error"
            status.message="保存时出现错误%s" %e
        return  status.status()
            
class edit():
    def GET(self,user_id):
        edit_user=DBSession.query(User).filter(User.user_id==user_id).first()
        group=DBSession.query(Group).order_by(Group.group_code)
        return view.user_edit(user=edit_user,g=group)
    def POST(self,name):
        u=web.input(is_lock='N',user_pwd=None)
        try:
            edit_user=DBSession.query(User).filter(User.user_id==u.user_id).first()
            if u.user_pwd<>None or u.user_pwd<>u'':edit_user.user_pwd=hash_password(u.user_pwd)
            edit_user.user_name=u.user_name
            edit_user.user_email=u.user_email            
            edit_user.is_lock=u.is_lock
            if u.group_id<>u'':edit_user.group_id=u.group_id  
            DBSession.commit()
            DBSession.flush()
            status.statusCode="ok"
            status.message="保存成功"
            status.navTabId="user_list"
        except Exception,e:
            DBSession.rollback()
            print Exception,e
            status.statusCode="error"
            status.message="保存时出现错误%s" %e
        return  status.status()
class delete():
    def POST(self,user_id):
        try:
            edit_user=DBSession.query(User).filter(User.user_id==user_id).first()
            DBSession.delete(edit_user)
            DBSession.commit()
            DBSession.flush()
            status.statusCode="ok"
            status.message="保存成功"
            status.navTabId="user_list"
        except Exception,e:
            DBSession.rollback()
            print Exception,e
            status.statusCode="error"
            status.message="删除时出现错误%s" %e
        return  status.status()